/*
 * Project:     My Project Work, Copyright (C) 2011
 * Created at:  03.08.2011
 */
package de.myprojectwork.view;

import de.myprojectwork.model.Table;
import de.myprojectwork.presenter.DbToolPresenter;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Chris Holdschuh
 */
public class DbToolViewImpl extends JFrame implements DbToolView {

    private DbToolPresenter presenter;
    
    private JButton bAusfuehren;
    private JComboBox<Action> cbAktion;
    private JTextArea taMessages;

    public DbToolViewImpl(DbToolPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void initUi() {
        setTitle("DB-Tool - My Project Work");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        setSize(720, 340);
        setLocationRelativeTo(null);
        
        setLayout(new FlowLayout(FlowLayout.RIGHT));
        
        taMessages = new JTextArea(15, 60);
        taMessages.setLineWrap(true);
        taMessages.setWrapStyleWord(true);
        taMessages.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(taMessages);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane);
        
        cbAktion = new JComboBox<>(Action.values());
        add(cbAktion);
        
        bAusfuehren = new JButton("Ausführen");
        bAusfuehren.addActionListener(new AusfuehrenAction());
        add(bAusfuehren);
        
        setVisible(true);
    }

    @Override
    public void postMessage(String message) {
        if (taMessages.getText() != null && taMessages.getText().length() > 0) {
            taMessages.append("\n");
        }
        taMessages.append(message);
    }
    
    public enum Action {
        
        SHOW_TABLES                            ("Zeige alle Tabelle", null),
        INSERT_TEST_DATA                       ("Testdaten einfügen", null),
        SELECT_TEST_DATA                       ("Testdaten auslesen", null), 
        
        CREATE_SEQUENCE                        ("Erstelle ID-Sequenz", null),
        CREATE_TAB_ACTIVITY_CATEGORY           ("Erstelle Tabelle " + Table.ACTIVITY_CATEGORY, Table.ACTIVITY_CATEGORY), 
        CREATE_TAB_CONFIGURATION               ("Erstelle Tabelle " + Table.CONFIGURATION, Table.CONFIGURATION), 
        CREATE_TAB_HISTORY_ACTIVITY            ("Erstelle Tabelle " + Table.HISTORY_ACTIVITY, Table.HISTORY_ACTIVITY),
        CREATE_TAB_TAG                         ("Erstelle Tabelle " + Table.TAG, Table.TAG),
        CREATE_TAB_LINK                        ("Erstelle Tabelle " + Table.LINK, Table.LINK),
        CREATE_TAB_PROJECT                     ("Erstelle Tabelle " + Table.PROJECT, Table.PROJECT),
        CREATE_TAB_RADAR                       ("Erstelle Tabelle " + Table.RADAR, Table.RADAR),
        CREATE_TAB_TODO                        ("Erstelle Tabelle " + Table.TODO, Table.TODO),
        CREATE_ALL_TAB                         ("Erstelle alle Tabellen", null),
        
        DEL_ALL_ENTRIES                        ("Lösche alle Einträge", null),
        DEL_SEQUENCE                           ("Lösche ID-Sequenz", null),
        DEL_TAB_ACTIVITY_CATEGORY              ("Lösche Tabelle " + Table.ACTIVITY_CATEGORY, Table.ACTIVITY_CATEGORY), 
        DEL_TAB_CONFIGURATION                  ("Lösche Tabelle " + Table.CONFIGURATION, Table.CONFIGURATION), 
        DEL_TAB_HISTORY_ACTIVITY               ("Lösche Tabelle " + Table.HISTORY_ACTIVITY, Table.HISTORY_ACTIVITY),
        DEL_TAB_TAG                            ("Lösche Tabelle " + Table.TAG, Table.TAG),
        DEL_TAB_LINK                           ("Lösche Tabelle " + Table.LINK, Table.LINK),
        DEL_TAB_PROJECT                        ("Lösche Tabelle " + Table.PROJECT, Table.PROJECT),
        DEL_TAB_RADAR                          ("Lösche Tabelle " + Table.RADAR, Table.RADAR),
        DEL_TAB_TODO                           ("Lösche Tabelle " + Table.TODO, Table.TODO),
        DEL_ALL_TAB                            ("Lösche alle Tabellen", null),
        DEL_DB                                 ("Lösche Datenbank", null);
        
        private String description;
        private Table table;

        private Action(String description, Table table) {
            this.description = description;
            this.table = table;
        }
        
        public Table getTable() {
            return table;
        }

        @Override
        public String toString() {
            return description;
        }
    }
    
    private class AusfuehrenAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            presenter.executeAction((Action) cbAktion.getSelectedItem());
        }
    }
}
